package gui.edt.swingutilities;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

/*
 * SwingWorker
 * Timer
 * SwingUtilities
 */
public class GUI extends JFrame {

	private int x = 0;

	private static int h, m, s;

	static {
		int _h = Calendar.getInstance().get(Calendar.HOUR);
		int _m = Calendar.getInstance().get(Calendar.MINUTE);
		int _s = Calendar.getInstance().get(Calendar.SECOND);
		h = _h;
		m = _m;
		s = _s;
	}

	private JButton button;
	private JTextField textField;
	private JButton buttonStop;
	private JButton buttonContinue;
	private JLabel labeltime = new JLabel("Time");

	private String message;
	private boolean display = true;

	// private final int FRAME_WIDTH = 400;
	// private final int FRAME_HEIGHT = 100;
	private final int TEXT_FIELD_WIDTH = 400;
	private final String TEXT_BUTTON = "RUN";
	private int cpt = 0;

	public GUI() {
		// TODO Auto-generated constructor stub
		// setPreferredSize(new Dimension(FRAME_WIDTH, FRAME_HEIGHT));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Creates Components
		createComponent();

		// Adds Components
		addComponent();

		// Manages events
		manageEventButton();
		manageEventButtonStop();
		manageEventButtonContinue();

		pack();
		// setVisible(true);
	}

	private void createComponent() {
		button = new JButton(TEXT_BUTTON);

		buttonStop = new JButton("STOP");
		buttonStop.setEnabled(false);
		buttonContinue = new JButton("CONTINUE");
		buttonContinue.setEnabled(false);

		textField = new JTextField();
		Dimension dim = button.getPreferredSize();
		int height = dim.height;
		int width = TEXT_FIELD_WIDTH;
		textField.setPreferredSize(new Dimension(width, height));
		textField.setEditable(false);

		final JPanel panelD = new JPanel() {
			public void paint(java.awt.Graphics g) {
				super.paint(g);
				Graphics2D g2d = (Graphics2D) g;
				g2d.fillRect(0, 0, x, 30);
			};
		};
		panelD.setSize(new Dimension(100, labeltime.getHeight()));
		getContentPane().add(panelD, BorderLayout.SOUTH);
		int delay = 10;
		javax.swing.Timer timer = new javax.swing.Timer(delay,
				new ActionListener() {

					boolean way = true;

					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub
						if (x < panelD.getWidth() && way) {
							x++;
						} else {
							way = false;
							if (x > 0 && !way) {
								x--;
							} else {
								way = true;
							}
						}
						panelD.repaint();
					}
				});
		timer.start();
	}

	private void addComponent() {

		getContentPane().add(labeltime, BorderLayout.NORTH);
		JPanel panel = new JPanel();
		panel.add(textField);
		panel.add(buttonStop);
		panel.add(buttonContinue);
		panel.add(button);
		getContentPane().add(panel, BorderLayout.CENTER);
	}

	private void manageEventButton() {
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				buttonContinue.setEnabled(true);
				buttonStop.setEnabled(true);
				System.out.println("RUN " + ++cpt);
			}
		});
	}

	private void manageEventButtonStop() {
		buttonStop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// int currVal = new Integer(message).intValue();
				// currVal++;
				// message = "" + currVal;
				// textField.setText(message);
				SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {
					@Override
					protected Object doInBackground() throws Exception {
						// TODO Auto-generated method stub
						button.setBackground(Color.RED);
						sleep(5000);
						return null;
					}

					@Override
					protected void done() {
						button.setBackground(Color.GREEN);
					}
				};
				sw.execute();
			}
		});
	}

	private void sleep(final int time) {
		try {
			Thread.sleep(time);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

	private static void flickV1(final JButton button) {
		SwingUtilities.invokeLater(new Runnable() {

			@Override
			public void run() {
				while (true) {
					button.setBackground(Color.GREEN);
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					button.setBackground(Color.RED);
				}
			}
		});
	}

	private void flickV2(final JButton button) {
		SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {

			@Override
			protected Object doInBackground() {
				button.setBackground(Color.GREEN);
				sleep(1000);
				button.setBackground(Color.RED);
				sleep(1000);
				return null;
			}

			protected void done() {
				button.setBackground(Color.RED);
				// try {
				// Thread.sleep(100);
				// System.out.println("RED WAIT");
				// } catch (InterruptedException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
			};
		};
		sw.execute();
	}

	private void manageEventButtonContinue() {
		buttonContinue.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
			}
		});
	}

	private class Task implements Runnable {

		@Override
		public void run() {
			// TODO Auto-generated method stub
			short cpt = 0;
			while (true) {
				message = "" + cpt;
				cpt++;
				// try {
				// Thread.sleep(1000);
				// } catch (InterruptedException e) {
				// // TODO Auto-generated catch block
				// e.printStackTrace();
				// }
				textField.setText(message);
			}
		}
	}

	public void doTask(final Thread thread) {
		message = "";
		SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() {

			@Override
			protected Object doInBackground() throws Exception {
				button.setBackground(Color.GREEN);
				return null;
			}

			@Override
			protected void done() {
				// TODO Auto-generated method stub
				thread.start();
			}
		};
		sw.execute();
	}

	private class Time implements Runnable {

		private int h, m, s;

		public void displayTime() {
			Calendar cal = Calendar.getInstance();

			h = cal.get(Calendar.HOUR);
			m = cal.get(Calendar.MINUTE);
			s = cal.get(Calendar.SECOND);

			while (true) {
				sleep(1000);
				if (s == 59) {
					s = 0;
					if (m == 59) {
						m = 0;
						if (h == 23) {
							h = 0;
						} else {
							h++;
						}
					} else {
						m++;
					}
				} else {
					s++;
				}
				labeltime.setText(format(h, m, s));
				System.out.println(labeltime.getText());
			}
		}

		private final String format(int h, int m, int s) {
			String formatS = "" + s, formatM = "" + m, formatH = "0" + h;
			if (s < 10) {
				formatS = "0" + s;
			}
			if (m < 10) {
				formatM = "0" + m;
			}
			if (h < 10) {
				formatH = "0" + h;
			}

			return formatH + ":" + formatM + ":" + formatS;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			displayTime();
		}
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				GUI gui = new GUI();
				Thread time = new Thread(gui.new Time());
				time.start();
				Thread t = new Thread(gui.new Task());
				gui.doTask(t);

				gui.setVisible(true);
			}
		});
	}
}
